#!/usr/bin/env python

import time
import component

class Protocol(component.Protocol):
    '''Protocol class
    
    @author Julot <andy.julot@gmail.com>
    @since Wednesday, July 3, 2013. 17:38 PM'''
    
    lastUpdate = None
    console = None
    
    def connectionMade(self):
        self.console = self.factory
        self.lastUpdate = time.time()
        self.console.clients.append(self)
        count = len(self.console.clients)
        s = 'New connection established. Total client: %s.\n'
        self.console.success(s % count)
    
    def connectionLost(self, reason):
        self.console.clients.remove(self)
        count = len(self.console.clients)
        self.console.warning('Connection lost. Total client: %s.\n' % count)
    
    def checkConnection(self):
        checkTime = 240
        now = time.time()
        elapsed = now - self.lastUpdate
        if elapsed >= checkTime:
            self.transport.loseConnection()
    